# -*- coding: utf-8 -*-
import os
import sys

# 基础路径配置
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
BASE_DIR = PROJECT_ROOT

# Django 核心配置
DEBUG = True
ALLOWED_HOSTS = ['*']
SECRET_KEY = 'your-secret-key-here-change-in-production'

# 第三方库路径配置
THIRD_PARTY_ROOT = os.path.join(PROJECT_ROOT, "thirdparty")
THEME_ROOT = os.path.join(PROJECT_ROOT, "theme")
sys.path.insert(0, THIRD_PARTY_ROOT)
sys.path.insert(0, THEME_ROOT)

# 应用配置 - 使用Django原生admin
INSTALLED_APPS = [
    # Django核心应用
    "django.contrib.admin",        # Django原生admin - 替代xadmin
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.sites",
    "django.contrib.messages",
    "django.contrib.humanize",
    "django.contrib.staticfiles",
    
    # 主题应用
    "theme.201504",
    
    # 第三方应用（已修复Django 5.1兼容性）
    "notification",
    "mailer",
    "emailconfirmation", 
    "announcements",
    
    # MES核心业务应用
    "about",
    "profiles",
    "blog",
    "person",
    "customer", 
    "companydepartment",
    "yt_file",
    "yt_timesheet",
    "yt_log",
    "warehouse",
    "productcatalog",
    "material",
    "device",
    "quality",
    "maintenance",
    "yt_report"
]

# 中间件配置
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

# URL配置
ROOT_URLCONF = 'urls'

# 数据库配置
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'walmart',
        'USER': 'walmart_user', 
        'PASSWORD': 'Zb_200407',
        'HOST': '175.27.138.86',
        'PORT': '3306',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
            'charset': 'utf8mb4',
        },
    }
}

# 模板配置
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
            os.path.join(THIRD_PARTY_ROOT, 'templates'),
            os.path.join(THEME_ROOT, 'templates'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.static',
                'django.template.context_processors.media',
                'django.template.context_processors.i18n',
            ],
        },
    },
]

# 静态文件配置
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
    os.path.join(THIRD_PARTY_ROOT, 'static'),
    os.path.join(THEME_ROOT, 'static'),
]

# 媒体文件配置
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# 国际化配置
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = True

# 站点配置
SITE_ID = 1

# 默认主键字段类型
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# 邮件配置
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

# Redis配置（用于缓存）
CONSTANCE_REDIS_CONNECTION = {
    'host': 'localhost',
    'port': 6379,  # 使用Redis默认端口
    'db': 0,
}

# Admin界面自定义
ADMIN_SITE_HEADER = "MES 制造执行系统"
ADMIN_SITE_TITLE = "MES Admin"
ADMIN_INDEX_TITLE = "欢迎使用 MES 管理系统"

# 日志配置
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': os.path.join(BASE_DIR, 'logs', 'django.log'),
        },
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file', 'console'],
            'level': 'INFO',
            'propagate': True,
        },
    },
}

# 确保日志目录存在
os.makedirs(os.path.join(BASE_DIR, 'logs'), exist_ok=True)

print("✅ 使用Django 5.1原生Admin系统 - 已移除xadmin/xladmin依赖")

